let app = getApp();
Component({
  properties: {
    parent: {
      type: Object,
      value: {},
    },
    isWebView: {
      type: Boolean,
      value: false,
    },
  },
  data: {
    attachment_url: '',
    noPic: false,
  },
  /**
   * 组件的方法列表
   * 初始化屏幕与热区块之间的比例
   * 计算当前屏幕中热区的位置，将计算完成的新区块值，交给点击事件判断
   */
  ready(e) {
    let self = this;
    if (this.data.parent === '' || this.data.parent === null || this.data.parent.imageSrc === null || this.data.parent.imageSrc === '') {
      this.setData({
        noPic: true,
      });
    }
    // var imgUrl = self.data.parent.imageSrc;
    // 获取系统宽高，并计算宽高比
    wx.getSystemInfo({
      success: function (res) {
        let windowWidth = res.windowWidth;
        let windowHeight = res.windowHeight;
        self.setData({ windowWidth, windowHeight });
      },
    });

  },
  methods: {
    /**
     * 确定触点的文章
     * 返回文章的href
     * pagenum代表版面号（从1开始编号）
     */
    getArticleHref: function (fingerx, fingery, pagenum) {
      let self = this;
      let currentPageAreas = self.data.paperInfo[pagenum - 1].areaArray;
      let aresLength = currentPageAreas.length;
      let i;
      let points = [];
      for (i = 0; i < aresLength; i++) {
        points = currentPageAreas[i].coords;
        if (self.pointInRegin([fingerx, fingery], points)) {
          return currentPageAreas[i].href;
        }
      }
    },
    /**
    * 点击跳转到相关文章
    * 记得在paper.wxml文件的img组件上绑定toArticle事件
    */
    toArticle: function (e) {
      let self = this;
      let fingerx = e.detail.x;
      let fingery = e.detail.y - e.currentTarget.offsetTop; // y方向要减去距离文档上方距离
      // 获取触点所在的文章href，目前只有第一版所以pagenum为1
      let href = self.getArticleHref(fingerx, fingery, 1);
      if (href) {
        self.triggerEvent(
          'toArticle',
          {
            href: href,
          },
          {
            bubbles: false,
            composed: false,
            capturePhase: false,
          }
        );
      }
    },
    // point为用户触点的坐标，coords为某个热区顶点情况
    pointInRegin: function (point, coords) {
      // coords中每两个是一对坐标
      let x = point[0];// 用户触点x位置
      let y = point[1];// 用户触点y位置
      let xi = coords[0];
      let yi = coords[1];
      let xj = coords[2];
      let yj = coords[3];
      if (yi == yj) { return false }// 如果两点水平，则跳过
      if (y < Math.min(yi, yj) || y > Math.max(yi, yj)) { return false }// 如果触点低于该线段或高于该线段，则跳过
      if (x >= Math.max(xi, xj)) { return false }// 如果触点在该线段的右边，则跳过
      // 判断点是否在热图区域内
      if (x > xi && x < xj && y > yi && y < yj) {
        return true;
      } else {
        return false;
      }
    },

    // 判断跳转类型，传值给herf
    pagePathHandler: function (linkObj) {
      return linkObj;
    },

    // 通过load事件，获取图片实际的宽高，在这里面进行点的计算
    imageLoad: function (e) {
      // console.log(e)
      let self = this;
      this.setData({
        imgWidth: e.detail.width,
        imgHeight: e.detail.height,
      });
      let point = self.data.parent.hotAreas;
      point.forEach((e, index) => {
        if (e.link && e.link.coords) {
          e.coords = e.link.coords.map(function (coord, index) {
            // 计算适合该设备的coords大小
            let tmpCoord = parseInt(coord, 10);
            if (index % 2 == 1) { // 代表y坐标
              tmpCoord = Math.ceil(tmpCoord * (self.data.windowWidth / self.data.imgWidth));
            }
            if (index % 2 == 0) { // 代表x坐标
              tmpCoord = Math.ceil(tmpCoord * (self.data.windowWidth / self.data.imgWidth));
            }
            // console.log(tmpCoord)
            return tmpCoord;
          });
        }
        // 处理pagePath数据
        if (e.link && e.link.linkInfo) {
          e.href = self.pagePathHandler(e.link.linkInfo);
        }
      });
      // console.log('point===================>',point)
      self.setData({
        paperInfo: [{ areaArray: point }],
        parent: self.data.parent,
      });
      // console.log('self.data.parent===================>',self.data.parent)
    },

    _makePhoneCall(e) {
      let { phonenumber } = e.currentTarget.dataset;
      let { mobile, landline } = app.util.phoneExp;
      if (mobile.test(String(phonenumber)) || landline.test(String(phonenumber))) {
        wx.makePhoneCall({
          phoneNumber: String(phonenumber),
          success: res => {
            console.log('wx.makePhoneCall success:', res);
          },
          fail: res => {
            console.log('wx.makePhoneCall fail:', res);
          },
        });
      } else {
        console.log('电话号码错误 ======= ', phonenumber);
      }
    },
  },
});
